/*
 * Copyright 2009 Andreas Borglin andreas.borglin@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package borge.mobile.app.jakoon;

import borge.mobile.lib.pimp.ImageConstants;
import org.eclipse.swt.graphics.Rectangle;
/**
 * Helper class for handling different modes
 * @author Andreas Borglin
 */
public class CanvasUtil implements ImageConstants{

    private static final int MAX_COORD_DIFF = 10;

    protected CanvasUtil() {}

    protected boolean inExternalHandlerMode(){
        if(Data.commandMode == BRIGHTNESS || Data.commandMode == CONTRAST
                || Data.commandMode == WHITEBALANCE)
            return true;

        return false;
    }

    protected boolean inChoiceArrayMode(){
        if(Data.commandMode == EFFECT ||
                Data.commandMode == ROTATE || Data.commandMode == FLIP){
            return true;
        }
        
        return false;
    }
    

    protected boolean inUserDrawMode(){
        if(inUserDrawRectMode() || inUserDrawLineMode())
            return true;

        return false;
    }

    protected boolean inUserDrawRectMode(){
        
        if(Data.commandMode == OVERLAY || Data.commandMode == CROP || Data.commandMode == FRAME)
            return true;
        
        return false;
        
    }

    protected boolean inUserDrawLineMode(){
        // Currently disabled
        //if(mode == ROTATE || mode == FLIP)
            //return true;

        return false;
    }
    
    protected boolean inSameArea(int x1, int x2){
        
        if(x2 - x1 > MAX_COORD_DIFF)
            return false;
        
        if(x1 - x2 > MAX_COORD_DIFF)
            return false;
        
        return true;
        
    }

    protected static Rectangle scaledSize(int iw, int ih, int aw, int ah){

        if (iw < aw && ih < ah) {
            return new Rectangle(0,0,iw,ih);
        }


        // TODO: Replace with a smarter algorithm

        double scaleFactor;

        int larger = iw;
        int smaller = ih;
        int largeSide = aw;
        int smallSide = ah;

        if ((iw - aw) < (ih - ah)) {
            larger = ih;
            smaller = iw;
            largeSide = ah;
            smallSide = aw;
        }

        scaleFactor = (double) larger / (double) smaller;
        int scaleSmall = (int) (largeSide / scaleFactor);
        int scaleLarge = largeSide;

        if (scaleSmall > smallSide) {
            scaleSmall = smallSide;
            scaleLarge = (int) (scaleSmall * scaleFactor);
        }

        if (largeSide == aw) {
            return new Rectangle(0,0,scaleLarge,scaleSmall);
        }
        else {
            return new Rectangle(0,0,scaleSmall,scaleLarge);
        }
    }
    

}
